close all;
clear all;

Medicion1=dlmread('Medicion1.txt','\t',1,0);
Medicion2=dlmread('Medicion2.txt','\t',1,0);
Medicion3=dlmread('Medicion3.txt','\t',1,0);
Simulacion1=dlmread('ICvsVBE(VCE=1.25V)phillips.txt','\t',1,0);
Simulacion2=dlmread('ICvsVBE(VCE=1.25)SIE.txt','\t',1,0);
Simulacion3=dlmread('tbjpropio(ICvsVBE).txt','\t',1,0);


Simulacion1(:,2)=Simulacion1(:,2)*1000;	% Normalización a mA
Simulacion2(:,2)=Simulacion2(:,2)*1000;
Simulacion3(:,2)=Simulacion3(:,2)*1000;


Medicion1Ajuste=Medicion1;
Medicion1Ajuste(:,2)=log(Medicion1Ajuste(:,2));

Param1=fmins('A_x_mas_B',[35 -23],[0,0.0001,0,0,0,0,0,0,0,],[],Medicion1Ajuste(3:end,:)');	
Vth1_1=1/Param1(1)
Is1_1=exp(Param1(2))

Param2=fmins('A_exp_B_x',[1.0634e-10 35],[0,0.0001,0,0,0,0,0,0,0,],[],Medicion1(3:end,:)');
Vth1_2=1/Param2(2)
Is1_2=Param2(1)


Medicion2Ajuste=Medicion2;
Medicion2Ajuste(:,2)=log(Medicion2Ajuste(:,2));

Param1=fmins('A_x_mas_B',[35 -23],[0,0.0001,0,0,0,0,0,0,0,],[],Medicion2Ajuste(3:end,:)');	
Vth2_1=1/Param1(1)
Is2_1=exp(Param1(2))

Param2=fmins('A_exp_B_x',[1.0634e-10 35],[0,0.0001,0,0,0,0,0,0,0,],[],Medicion2(3:end,:)');
Vth2_2=1/Param2(2)
Is2_2=Param2(1)



Medicion3Ajuste=Medicion3;
Medicion3Ajuste(:,2)=log(Medicion3Ajuste(:,2));

Param1=fmins('A_x_mas_B',[35 -23],[0,0.0001,0,0,0,0,0,0,0,],[],Medicion3Ajuste(3:end,:)');	
Vth3_1=1/Param1(1)	
Is3_1=exp(Param1(2))

Param2=fmins('A_exp_B_x',[1.0634e-10 35],[0,0.0001,0,0,0,0,0,0,0,],[],Medicion3(3:end,:)');
Vth3_2=1/Param2(2)
Is3_2=Param2(1)



Simulacion1Ajuste=Simulacion1;
Simulacion1Ajuste(:,2)=log(Simulacion1Ajuste(:,2));

Param1=fmins('A_x_mas_B',[38 -25],[0,0.0001,0,0,0,0,0,0,0,],[],Simulacion1Ajuste(8:end-11,:)');	
Vth4_1=1/Param1(1)
Is4_1=exp(Param1(2))

Param2=fmins('A_exp_B_x',[1.0634e-10 35],[0,0.0001,0,0,0,0,0,0,0,],[],Simulacion1(8:end-11,:)');
Vth4_2=1/Param2(2)
Is4_2=Param2(1)


Simulacion2Ajuste=Simulacion2;
Simulacion2Ajuste(:,2)=log(Simulacion2Ajuste(:,2));

Param1=fmins('A_x_mas_B',[38 -25],[0,0.0001,0,0,0,0,0,0,0,],[],Simulacion2Ajuste(14:end-20,:)');	
Vth5_1=1/Param1(1)	
Is5_1=exp(Param1(2))
	
Param2=fmins('A_exp_B_x',[1.0634e-10 35],[0,0.0001,0,0,0,0,0,0,0,],[],Simulacion2(14:end-20,:)');
Vth5_2=1/Param2(2)
Is5_2=Param2(1)


Simulacion3Ajuste=Simulacion3;
Simulacion3Ajuste(:,2)=log(Simulacion3Ajuste(:,2));

Param1=fmins('A_x_mas_B',[38 -25],[0,0.0001,0,0,0,0,0,0,0,],[],Simulacion3Ajuste(14:end-20,:)');	
Vth6_1=1/Param1(1)	
Is6_1=exp(Param1(2))
	
Param2=fmins('A_exp_B_x',[1.0634e-10 35],[0,0.0001,0,0,0,0,0,0,0,],[],Simulacion3(14:end-20,:)');
Vth6_2=1/Param2(2)
Is6_2=Param2(1)

sprintf('\n Primer transistor & %f & %e & %f & %e',Vth1_1, Is1_1, Vth1_2, Is1_2)
sprintf('\n Segundo transistor & %f & %e & %f & %e',Vth2_1, Is2_1, Vth2_2, Is2_2)
sprintf('\n Tercer transistor & %f & %e & %f & %e',Vth3_1, Is3_1, Vth3_2, Is3_2)
sprintf('\n Simulacion Phillips & %f & %e & %f & %e',Vth4_1, Is4_1, Vth4_2, Is4_2)
sprintf('\n Simulacion Siemens & %f & %e & %f & %e',Vth5_1, Is5_1, Vth5_2, Is5_2)
sprintf('\n Modelo propio & %f & %e & %f & %e',Vth6_1, Is6_1, Vth6_2, Is6_2)


V=0.4:0.01:0.9;

figure
hold on

semilogy(Medicion1(:,1),Medicion1(:,2),'ro','Markersize',6)
semilogy(V(1:end-20),Is1_1*exp(V(1:end-20)/Vth1_1),'r-','Linewidth',1)
semilogy(V(1:end-20),Is1_2*exp(V(1:end-20)/Vth1_2),'r-','Linewidth',3)

semilogy(Medicion2(:,1),Medicion2(:,2),'go','Markersize',6)
semilogy(V(1:end-20),Is2_1*exp(V(1:end-20)/Vth2_1),'g-','Linewidth',1)
semilogy(V(1:end-20),Is2_2*exp(V(1:end-20)/Vth2_2),'g-','Linewidth',3)

semilogy(Medicion3(:,1),Medicion3(:,2),'bo','Markersize',6)
semilogy(V(1:end-20),Is3_1*exp(V(1:end-20)/Vth3_1),'b-','Linewidth',1)
semilogy(V(1:end-20),Is3_2*exp(V(1:end-20)/Vth3_2),'b-','Linewidth',3)

semilogy(Simulacion1(:,1),Simulacion1(:,2),'m*','Markersize',6)
semilogy(V(1:end-20),Is4_1*exp(V(1:end-20)/Vth4_1),'m-','Linewidth',1)
semilogy(V(1:end-20),Is4_2*exp(V(1:end-20)/Vth4_2),'m-','Linewidth',3)

semilogy(Simulacion2(:,1),Simulacion2(:,2),'c*','Markersize',6)
semilogy(V(1:end-20),Is5_1*exp(V(1:end-20)/Vth5_1),'c-','Linewidth',1)
semilogy(V(1:end-20),Is5_2*exp(V(1:end-20)/Vth5_2),'c-','Linewidth',3)

semilogy(Simulacion3(:,1),Simulacion3(:,2),'k*','Markersize',6)
semilogy(V(1:end-15),Is6_1*exp(V(1:end-15)/Vth6_1),'k-','Linewidth',1)
semilogy(V(1:end-15),Is6_2*exp(V(1:end-15)/Vth6_2),'k-','Linewidth',3)


legend('1er transistor','ajuste lineal','ajuste exponencial',
	   '2do transistor','ajuste lineal','ajuste exponencial',
	   '3er transistor','ajuste lineal','ajuste exponencial',
	   'simulacion phillips','ajuste lineal','ajuste exponencial',
	   'simulacion siemens','ajuste lineal','ajuste exponencial',
	   'modelo propio','ajuste lineal','ajuste exponencial',
	   'Location','Southeast')


xlabel('Tension Vbe [V]')
ylabel('Corriente log(Ic) [mA]')
axis([0.2 1 0.000000001 100000])
grid minor
print('fig_IdvsVbe.png','-dpng');
